package com.ccx.coachsite.ui.tutorial;

import java.util.List;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.TutorialInfo;
import com.ccx.coachsite.bus.TutorialPageInfo;
import com.ccx.coachsite.ui.customview.MaskButton;
import com.ccx.coachsite.webimage.ImageLoader;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class TutorialPageDataLoader {

	private boolean loaded = false;

	private View view;
	private TutorialPageInfo page;
	private FrameLayout rootLayout;
	private FrameLayout tutorialLayout1, tutorialLayout2, tutorialLayout3,
			tutorialLayout4, tutorialLayout5;
	private Button btnReload;

	public TutorialPageDataLoader(View view, TutorialPageInfo page) {
		this.view = view;
		this.page = page;
		rootLayout = (FrameLayout) view.findViewById(R.id.root);
		tutorialLayout1 = (FrameLayout) view
				.findViewById(R.id.tutorial_layout1);
		tutorialLayout2 = (FrameLayout) view
				.findViewById(R.id.tutorial_layout2);
		tutorialLayout3 = (FrameLayout) view
				.findViewById(R.id.tutorial_layout3);
		tutorialLayout4 = (FrameLayout) view
				.findViewById(R.id.tutorial_layout4);
		tutorialLayout5 = (FrameLayout) view
				.findViewById(R.id.tutorial_layout5);
		btnReload = (Button) view.findViewById(R.id.btn_nodata_reflash);
		// tutorialLayout1.setOnClickListener(clickListener);
		// tutorialLayout2.setOnClickListener(clickListener);
		// tutorialLayout3.setOnClickListener(clickListener);
		// tutorialLayout4.setOnClickListener(clickListener);
		// tutorialLayout5.setOnClickListener(clickListener);
		btnReload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				TutorialPageDataLoader.this.loadData();
			}
		});

		switchView(R.id.ll_on_loading);
		// onLoadSuccess(page.getTutorialList());
	}

	public void loadData() {
		// switchView(R.id.ll_on_loading);
		if (!loaded) {
			onLoadSuccess(page.getTutorialList());
			loaded = true;
		}
	}

	private void switchView(int id) {
		for (int i = 0; i < rootLayout.getChildCount(); i++) {
			View view = rootLayout.getChildAt(i);
			if (view.getId() == id)
				view.setVisibility(View.VISIBLE);
			else
				view.setVisibility(View.GONE);
		}
	}

	private void onLoadSuccess(List<TutorialInfo> tutorials) {
		TutorialInfo ti = null;
		if (tutorials.size() > 0) {
			ti = tutorials.get(0);
			tutorialLayout1.setTag(ti);
			tutorialLayout1.removeAllViews();
			tutorialLayout1.addView(createView(ti, true));
			tutorialLayout1.setClickable(true);
			tutorialLayout1.setOnClickListener(clickListener);
		}
		if (tutorials.size() > 1) {
			ti = tutorials.get(1);
			tutorialLayout2.setTag(ti);
			tutorialLayout2.removeAllViews();
			tutorialLayout2.addView(createView(ti, false));
			tutorialLayout2.setClickable(true);
			tutorialLayout2.setOnClickListener(clickListener);
		}
		if (tutorials.size() > 2) {
			ti = tutorials.get(2);
			tutorialLayout3.setTag(ti);
			tutorialLayout3.removeAllViews();
			tutorialLayout3.addView(createView(ti, false));
			tutorialLayout3.setClickable(true);
			tutorialLayout3.setOnClickListener(clickListener);
		}
		if (tutorials.size() > 3) {
			ti = tutorials.get(3);
			tutorialLayout4.setTag(ti);
			tutorialLayout4.removeAllViews();
			tutorialLayout4.addView(createView(ti, false));
			tutorialLayout4.setClickable(true);
			tutorialLayout4.setOnClickListener(clickListener);
		}
		if (tutorials.size() > 4) {
			ti = tutorials.get(4);
			tutorialLayout5.setTag(ti);
			tutorialLayout5.removeAllViews();
			tutorialLayout5.addView(createView(ti, false));
			tutorialLayout5.setClickable(true);
			tutorialLayout5.setOnClickListener(clickListener);
		}
		switchView(R.id.ll_tutorial_list);
	}

	// private void onLoadFail() {
	// switchView(R.id.ll_no_data);
	// }

	private View createView(TutorialInfo info, boolean bigSize) {
		if (!bigSize) {
			View itemView = View.inflate(view.getContext(),
					R.layout.tutorial_text_button, null);
			TextView txtTitle = (TextView) itemView
					.findViewById(R.id.tutorial_button_title);
			TextView txtDate = (TextView) itemView
					.findViewById(R.id.tutorial_button_date);
			txtTitle.setText(info.getTitle());
			txtDate.setText(info.getDate());
			return itemView;
		} else {
			if (TextUtils.isEmpty(info.getImageUrl())) {
				View itemView = View.inflate(view.getContext(),
						R.layout.tutorial_text_button, null);
				TextView txtTitle = (TextView) itemView
						.findViewById(R.id.tutorial_button_title);
				TextView txtDate = (TextView) itemView
						.findViewById(R.id.tutorial_button_date);
				txtTitle.setTextSize(18);
				txtTitle.setText(info.getTitle());
				txtDate.setText(info.getDate());
				return itemView;
			} else {
				View itemView = View.inflate(view.getContext(),
						R.layout.tutorial_image_button, null);
				ImageView backImage = (ImageView) itemView
						.findViewById(R.id.tutorial_image_button_back_image);
				MaskButton foreButton = (MaskButton) itemView
						.findViewById(R.id.tutorial_image_button_fore_button);
				ImageLoader.loadBitmap(info.getImageUrl(), backImage);
				foreButton.setText(info.getTitle());
				foreButton.setTag(info);
				foreButton.setOnClickListener(clickListener);
				return itemView;
			}
		}
	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (v.getTag() != null) {
				TutorialInfo info = (TutorialInfo) v.getTag();
				Intent intent = new Intent(view.getContext(),
						TutorialDetailActivity.class);
				intent.putExtra("tutorial", info);
				view.getContext().startActivity(intent);
			}
		}
	};
}
